package com.sxfq.loansupermarket.backsystem.modular.weixin.config;

import java.text.SimpleDateFormat;

import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.weixin.controller.WeixinController;
import net.sf.json.JSONObject;
import com.sxfq.loansupermarket.backsystem.modular.weixin.entity.Data;
import com.sxfq.loansupermarket.backsystem.modular.weixin.entity.NewOrdersTemplate;
import com.sxfq.loansupermarket.backsystem.modular.weixin.entity.Data_style;

import java.util.Date;

/**
 * 微信开发者接入
 *
 * @author Administrator
 */
public class WechatSendConfig {
    //微信模板接口(预约提醒)
    public static void send_appointment_message(String openId, String treatmentUnit, String projectData, String doctor, String reservationsNum,
                                                String customerName, String startTimeValue, String reservationsId, String customerPhone) {
        //因为我申请的模板是需要填写当前时间戳的，所以在这里我获取了当前的时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH时mm分");
        String format = simpleDateFormat.format(new Date());
        WeixinController weixinController = new WeixinController();
        String access_token = weixinController.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
        String templateId = "E2z7GEAsVF1ccnhcVj10qZ6YO1saX0Maet5kL0_xpl0";//填写申请的模板id
        String goUrl = "http://test.yiyatest.cn:8787/integralMallWeixin/medicalRecords";//填写点击推送的消息需跳转到的url
        Data_style first = new Data_style();
        Data_style keyword1 = new Data_style();
        Data_style keyword2 = new Data_style();
        Data_style keyword3 = new Data_style();
        Data_style keyword4 = new Data_style();
        Data_style keyword5 = new Data_style();
        Data_style remark = new Data_style();

        NewOrdersTemplate temp = new NewOrdersTemplate();
        Data data = new Data();

        first.setValue(format);
        first.setColor("#173177");

        keyword1.setValue(customerName);
        keyword1.setColor("#173177");

        keyword2.setValue(reservationsNum);
        keyword2.setColor("#173177");

        keyword3.setValue(treatmentUnit + "(" + projectData + ")");
        keyword3.setColor("#173177");

        keyword4.setValue(doctor);
        keyword4.setColor("#173177");

        keyword5.setValue(startTimeValue);
        keyword5.setColor("#173177");

        remark.setValue(ShiroKit.getUser().getAddress() + "温馨提示：请您于就诊当日按照预约时间段提前到店等候，如需改期请与就诊前一日联系取消。");
        remark.setColor("#173177");
        data.setFirst(first);
        data.setKeyword1(keyword1);
        data.setKeyword2(keyword2);
        data.setKeyword3(keyword3);
        data.setKeyword4(keyword4);
        data.setKeyword5(keyword5);
        data.setRemark(remark);
        temp.setTouser(openId);
        temp.setTemplate_id(templateId);
        temp.setUrl(goUrl);
        temp.setTopcolor("#173177");
        temp.setData(data);
        String jsonString = JSONObject.fromObject(temp).toString().replace("day", "Day");
        com.alibaba.fastjson.JSONObject jsonObject = WebChatUtil.httpRequest(url, "POST", jsonString);
        System.out.println(jsonObject);
    }

    //微信模板接口(术后医嘱)
    public static void send_medicalAdvice_message(String openId, String doctor, String medicalAdviceTitle, String content) {
        //因为我申请的模板是需要填写当前时间戳的，所以在这里我获取了当前的时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH时mm分");
        String format = simpleDateFormat.format(new Date());
        WeixinController weixinController = new WeixinController();
        String access_token = weixinController.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
        String templateId = "ja5-Tcs9o1-8CMXo3Y9haQeN1gy6sC2vrQPCDsj7xco";//填写申请的模板id
        String goUrl = "http://test.yiyatest.cn:8787/integralMallWeixin/integralPushMessage/" + medicalAdviceTitle;//填写点击推送的消息需跳转到的url
        Data_style first = new Data_style();
        Data_style keyword1 = new Data_style();
        Data_style keyword2 = new Data_style();
        Data_style remark = new Data_style();
        NewOrdersTemplate temp = new NewOrdersTemplate();
        Data data = new Data();

        first.setValue(format);
        first.setColor("#173177");

        keyword1.setValue(doctor);
        keyword1.setColor("#173177");

        keyword2.setValue(medicalAdviceTitle);
        keyword2.setColor("#173177");

        remark.setValue(content.replace("<br>", ""));
        remark.setColor("#173177");

        data.setFirst(first);
        data.setKeyword1(keyword1);
        data.setKeyword2(keyword2);
        data.setRemark(remark);

        temp.setTouser(openId);
        temp.setTemplate_id(templateId);
        temp.setUrl(goUrl);
        temp.setTopcolor("#173177");
        temp.setData(data);

        String jsonString = JSONObject.fromObject(temp).toString().replace("day", "Day");
        com.alibaba.fastjson.JSONObject jsonObject = WebChatUtil.httpRequest(url, "POST", jsonString);
        System.out.println(jsonObject);
    }
}